package com;

import com.mapper.WorkerMapper;
import com.pojo.Customer;
import com.pojo.Student;
import com.pojo.Worker;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import util.MybatisUtils;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

public class MyBatisTest {
    @Test
    public void findCustomerByNameAndJobsTest(){
        SqlSession sqlSession = MybatisUtils.getSession();
        Customer customer = new Customer();
        customer.setUsername("jack");
        customer.setJobs("teacher");
        List<Customer> customers = sqlSession.
                selectList("com.mapper.CustomerMapper.findCustomerByNameAndJobs", customer);
        for (Customer customer1 : customers) {
            System.out.println(customer1.toString());
        }
        sqlSession.close();
        sqlSession.close();
    }
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;
    @Before
    public void init() {
        // 定义读取文件名
        String resources = "mybatis-config.xml";
        // 创建流
        Reader reader = null;
        try {
            // 读取mybatis-config.xml文件到reader对象中
            reader = Resources.getResourceAsReader(resources);
            // 初始化mybatis,创建SqlSessionFactory类的对象
            SqlSessionFactory sqlMapper = new
                    SqlSessionFactoryBuilder().build(reader);
            // 创建sqlSessionn对象
            sqlSession = sqlMapper.openSession();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void findAllStudentTest() {
        // SqlSession执行映射文件中定义的SQL，并返回映射结果
        List<Student> list =
                sqlSession.selectList("findAllStudent");
        for (Student student : list) {
            System.out.println(student);
        }
    }
    @After
    public void destory() {
        // 提交事务
        sqlSession.commit();
        // 关闭事务
        sqlSession.close();
    }
    @Test
    public void findWorkerByIdTest() {
        SqlSession session = MybatisUtils.getSession();
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        Worker worker = mapper.selectWorker(1);
        System.out.println(worker.toString());
        session.close();
    }
    @Test
    public void insertWorkerTest() {
        SqlSession session = MybatisUtils.getSession();
        Worker worker = new Worker();
        worker.setId(4);
        worker.setName("赵六");
        worker.setAge(36);
        worker.setSex("女");
        worker.setworker_id(1004);
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        int result = mapper.insertWorker(worker);
        if (result > 0) {
            System.out.println("成功插入" + result+ "条数据！");
        }else {
            System.out.println("插入数据失败");
        }
        System.out.println(worker.toString());
        session.commit();
        session.close();
    }
    @Test
    public void updateWorkerTest() {
        SqlSession session = MybatisUtils.getSession();
        Worker worker = new Worker();
        worker.setId(4);
        worker.setName("李华");
        worker.setAge(28);
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        int result = mapper.updateWorker(worker);
        if (result > 0) {
            System.out.println("成功更新" + result+ "条数据！");
        }else{
            System.out.println("更新数据失败");
        }
        System.out.println(worker.toString());
        session.commit();
        session.close();
    }
    @Test
    public void deleteWorkerTest() {
        SqlSession session = MybatisUtils.getSession();
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        int result = mapper.deleteWorker(4);
        if (result > 0) {
            System.out.println("成功删除" + result+ "条数据！");
        }else {
            System.out.println("删除数据失败");
        }
        session.commit();
        session.close();
    }
}

